package models;
import java.text.Format;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.*;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Page;
import play.data.format.Formats;
import play.data.validation.Constraints;
import play.db.ebean.Model;
import controllers.JsonDateFormatter;
import controllers.JsonDatetimeFormatter;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import controllers.Helper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import play.libs.Json;
import models.Booking;
import models.Vehicle;
import models.Driver;
import models.Customer;
@Entity
@Table(name="dispatch")
public class Dispatch extends Model {
	private static final long serialVersionUID = 1L;
	@Id
	public Integer id; //编号
	@ManyToOne
	public Booking book; //预约号
	@ManyToOne
	public Vehicle vehicle; //车牌号
	@ManyToOne
	public Driver driver; //司机
	
	public String cai_ci; //差次
	
	public String follow_name; //随车人
	
	public Integer follow_phone; //联系电话
	@ManyToOne
	public Customer customer; //用车客户
	
	public Integer passenger_number; //乘员数
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date start_date; //用车日期
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date end_date; //结束日期
	
	public Integer plan_route; //计划行程
	
	public String stop_place; //停车地点
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date stop_time; //停车时间
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date end_datetime; //结束时间
	
	public Integer limit_mileage; //额定里程
	
	public Double limit_price; //额定车价
	
	public Double parking_fees; //停车费(估)
	
	public Double passroad_fees; //过路费（估）
	
	public String payer; //承担方
	
	public Integer allowance; //补贴
	
	public String checkout_type; //结算方式
	
	public String work_type; //业务性质
	
	public String name; //承接人
	
	public Integer check_number; //核定数
	
	public String comment; //备注
	
	public String out_province; //出省登记
	
	public String start_remind; //发车提醒
	
	public String leave_place; //出场地点
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date leave_time; //出场时间
	
	public String back_place; //回场地点
	@JsonSerialize(using=JsonDatetimeFormatter.class)
	public Date back_time; //回场时间
	
	public String change_comment; //变更说明
	
	public String borrow_customer; //借车单位
	
	public Integer borrow_charge; //借车费
	
	public Integer checkout_charge; //结算费
	
	public Integer confirm; //费用确认
	
	public String cancel; //业务取消
	@JsonSerialize(using=JsonDateFormatter.class)
	public Date dispatch_date; //派车日期
	public static Model.Finder<Integer, Dispatch> find = new Model.Finder<Integer, Dispatch>(Integer.class, Dispatch.class);

	public static Page<Dispatch> page(int page, int pageSize, String sortBy, String order, String [] filterColum, String[] filter) {
		ExpressionList<Dispatch> exp = find.where();
		for(int i= 0; i< filterColum.length; i++) {
			String[] tokens = filter[i].split("\\|", 3);
			if (tokens.length == 0) {
				continue;
			}
			String fieldtype = tokens[0];
			if (tokens.length == 3) { //two values, range search
				if (tokens[1].isEmpty() || tokens[2].isEmpty() ) {
					continue;
				}
				Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
				Object obj1 = Helper.stringToObject(fieldtype, tokens[2]);
				exp = exp.between(filterColum[i], obj0, obj1);
			} else if (tokens.length == 2) { //one value search
				if (tokens[1].isEmpty()) {
					continue;
				}
				if (fieldtype.equalsIgnoreCase("String")) {
					exp = exp.ilike(filterColum[i], "%" + tokens[1] + "%");
				} else {
					Object obj0 = Helper.stringToObject(fieldtype, tokens[1]);
					exp = exp.eq(filterColum[i], obj0);
				}
			}
		}
		return exp.orderBy(sortBy + " " + order).findPagingList(pageSize).setFetchAhead(false).getPage(page);
	}
	public void setId(Integer d) {
		id= d;
	}
	public Integer getId() {
		return id;
	}
	public void setBook(Booking d) {
		book= d;
	}
	public Booking getBook() {
		return book;
	}
	public void setVehicle(Vehicle d) {
		vehicle= d;
	}
	public Vehicle getVehicle() {
		return vehicle;
	}
	public void setDriver(Driver d) {
		driver= d;
	}
	public Driver getDriver() {
		return driver;
	}
	public void setCai_ci(String d) {
		cai_ci= d;
	}
	public String getCai_ci() {
		return cai_ci;
	}
	public void setFollow_name(String d) {
		follow_name= d;
	}
	public String getFollow_name() {
		return follow_name;
	}
	public void setFollow_phone(Integer d) {
		follow_phone= d;
	}
	public Integer getFollow_phone() {
		return follow_phone;
	}
	public void setCustomer(Customer d) {
		customer= d;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setPassenger_number(Integer d) {
		passenger_number= d;
	}
	public Integer getPassenger_number() {
		return passenger_number;
	}
	public void setStart_date(Date d) {
		start_date= d;
	}
	public Date getStart_date() {
		return start_date;
	}
	public void setEnd_date(Date d) {
		end_date= d;
	}
	public Date getEnd_date() {
		return end_date;
	}
	public void setPlan_route(Integer d) {
		plan_route= d;
	}
	public Integer getPlan_route() {
		return plan_route;
	}
	public void setStop_place(String d) {
		stop_place= d;
	}
	public String getStop_place() {
		return stop_place;
	}
	public void setStop_time(Date d) {
		stop_time= d;
	}
	public Date getStop_time() {
		return stop_time;
	}
	public void setEnd_datetime(Date d) {
		end_datetime= d;
	}
	public Date getEnd_datetime() {
		return end_datetime;
	}
	public void setLimit_mileage(Integer d) {
		limit_mileage= d;
	}
	public Integer getLimit_mileage() {
		return limit_mileage;
	}
	public void setLimit_price(Double d) {
		limit_price= d;
	}
	public Double getLimit_price() {
		return limit_price;
	}
	public void setParking_fees(Double d) {
		parking_fees= d;
	}
	public Double getParking_fees() {
		return parking_fees;
	}
	public void setPassroad_fees(Double d) {
		passroad_fees= d;
	}
	public Double getPassroad_fees() {
		return passroad_fees;
	}
	public void setPayer(String d) {
		payer= d;
	}
	public String getPayer() {
		return payer;
	}
	public void setAllowance(Integer d) {
		allowance= d;
	}
	public Integer getAllowance() {
		return allowance;
	}
	public void setCheckout_type(String d) {
		checkout_type= d;
	}
	public String getCheckout_type() {
		return checkout_type;
	}
	public void setWork_type(String d) {
		work_type= d;
	}
	public String getWork_type() {
		return work_type;
	}
	public void setName(String d) {
		name= d;
	}
	public String getName() {
		return name;
	}
	public void setCheck_number(Integer d) {
		check_number= d;
	}
	public Integer getCheck_number() {
		return check_number;
	}
	public void setComment(String d) {
		comment= d;
	}
	public String getComment() {
		return comment;
	}
	public void setOut_province(String d) {
		out_province= d;
	}
	public String getOut_province() {
		return out_province;
	}
	public void setStart_remind(String d) {
		start_remind= d;
	}
	public String getStart_remind() {
		return start_remind;
	}
	public void setLeave_place(String d) {
		leave_place= d;
	}
	public String getLeave_place() {
		return leave_place;
	}
	public void setLeave_time(Date d) {
		leave_time= d;
	}
	public Date getLeave_time() {
		return leave_time;
	}
	public void setBack_place(String d) {
		back_place= d;
	}
	public String getBack_place() {
		return back_place;
	}
	public void setBack_time(Date d) {
		back_time= d;
	}
	public Date getBack_time() {
		return back_time;
	}
	public void setChange_comment(String d) {
		change_comment= d;
	}
	public String getChange_comment() {
		return change_comment;
	}
	public void setBorrow_customer(String d) {
		borrow_customer= d;
	}
	public String getBorrow_customer() {
		return borrow_customer;
	}
	public void setBorrow_charge(Integer d) {
		borrow_charge= d;
	}
	public Integer getBorrow_charge() {
		return borrow_charge;
	}
	public void setCheckout_charge(Integer d) {
		checkout_charge= d;
	}
	public Integer getCheckout_charge() {
		return checkout_charge;
	}
	public void setConfirm(Integer d) {
		confirm= d;
	}
	public Integer getConfirm() {
		return confirm;
	}
	public void setCancel(String d) {
		cancel= d;
	}
	public String getCancel() {
		return cancel;
	}
	public void setDispatch_date(Date d) {
		dispatch_date= d;
	}
	public Date getDispatch_date() {
		return dispatch_date;
	}
	public static Dispatch findById(Integer d) {
		return find.byId(d);
	}
	public static String getIdJson() {
		ObjectNode node = Json.newObject();
		node.put("", "");
		for(Dispatch v: find.all()) {
			node.put(v.getId().toString(), v.getId().toString()); 
		}
		return node.toString();
	}
}
